package com.jyy.prefabricated.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.binarywang.wxpay.bean.ecommerce.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.jyy.prefabricated.entity.OrderInfo;
import com.jyy.prefabricated.entity.PayAccount;
import com.jyy.prefabricated.mapper.PayAccountMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PayService {

    @Autowired
    private WxPayService wxPayService;

    @Resource
    private PayAccountMapper payAccountDao;

    /**
     * 微信统一下单
     * @param orderInfo 订单信息
     * @param tradeType 下单方式
     * @return
     * @throws WxPayException
     */
    public WxPayUnifiedOrderResult tradePay(OrderInfo orderInfo,String tradeType,String ip,String teamCode) throws WxPayException {
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setOutTradeNo(orderInfo.getOrderCode());
        request.setSpbillCreateIp(ip);
        request.setTradeType(tradeType);

        if(WxPayConstants.TradeType.MWEB.equals(tradeType)){
            Map<String,Object> sceneInfoMap = new HashMap<>();
            sceneInfoMap.put("payer_client_ip", ip);
            Map<String,Object> h5Info = new HashMap<>();
            h5Info.put("type","Wap");
            sceneInfoMap.put("h5_info",h5Info);
            String sceneInfo = JSON.toJSONString(sceneInfoMap, SerializerFeature.WriteMapNullValue);
            request.setSceneInfo(sceneInfo);
        } else if(WxPayConstants.TradeType.JSAPI.equals(tradeType)){
            request.setOpenid(orderInfo.getOpenid());
        }
        else if(WxPayConstants.TradeType.NATIVE.equals(tradeType)){

        }
        request.setBody(orderInfo.getProductName());
        request.setProductId(String.valueOf(orderInfo.getProductId()));
        Integer totalFee = orderInfo.getTotalPrice().multiply(new BigDecimal("100")).intValue();
        request.setTotalFee(totalFee);
        WxPayService payService = WxMerchantsService.merchantsService.get(teamCode);

        return payService.unifiedOrder(request);
    }

    /**
     * js统一下单
     * @param orderInfo 订单信息
     * @param tradeType 支付类型
     * @param ip ip地址
     * @return
     * @throws WxPayException
     */
    public Map<String,String> tradePayJSAPI(OrderInfo orderInfo,String tradeType,String ip) throws WxPayException {
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setOutTradeNo(orderInfo.getOrderCode());
        request.setSpbillCreateIp(ip);
        request.setTradeType(tradeType);
        request.setSignType(WxPayConstants.SignType.MD5);
        request.setOpenid(orderInfo.getOpenid());
        request.setBody(orderInfo.getProductName());
        request.setProductId(String.valueOf(orderInfo.getProductId()));
        Integer totalFee = orderInfo.getTotalPrice().multiply(new BigDecimal("100")).intValue();
        request.setTotalFee(totalFee);
        return wxPayService.getPayInfo(request);
    }
}
